package se.cbb.scripts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;

import se.cbb.misc.SequenceFileWriter;

public class ExtractConvergence {

	public static void main(String[] args) {
		try{ 
			String path = "/Users/rhali/Movies/Projects/VMCMC/simRuns/simRun3/MCMC/Uninformed/Convergence/";
			File folder = new File(path);
			File[] listOfFiles = folder.listFiles();
			int numberofFiles = listOfFiles.length;
			ArrayList<String> list = new ArrayList<String>();
			
			for(int i = 0; i < numberofFiles; i++) {
				if(listOfFiles[i].getName().endsWith(".out")) {
					System.out.println("I was here");
					list.add(listOfFiles[i].getName());
				}
			}
			String fileName = "ConvergenceEstimates.txt";
			for(int i = 0; i < list.size(); i++) {
				BufferedReader bufferedreader = new BufferedReader(new FileReader(path + list.get(i)));
				String str = bufferedreader.readLine();
				str = bufferedreader.readLine();
				str = str.replaceAll("\\s+","");
				String name = str;
				while ((str = bufferedreader.readLine()) != null) {
					str = str.replaceAll("\\s+","");
					if(str.contains("\"ESS")) {
						SequenceFileWriter.writeAndAppendLine(path, fileName, name + "\tESS_max\t" + str);
					} else if(str.contains("Geweke")) {
						SequenceFileWriter.writeAndAppendLine(path, fileName, name + "\tGeweke_max\t" + str);
					} else if(str.contains("Gelman_Rubin")) {
						SequenceFileWriter.writeAndAppendLine(path, fileName, name + "\tGelman_Rubin_max\t" + str);
					} else if(str.contains("Standardized")) {
						SequenceFileWriter.writeAndAppendLine(path, fileName, name + "\tStandardized_ESS\t" + str);
					} else if(str.contains("Normalized")) {
						SequenceFileWriter.writeAndAppendLine(path, fileName, name + "\tNormalized_ESS\t" + str);
					}
				}
				bufferedreader.close();
			}
						
		} catch (Exception e) {
			System.out.println("Error: " + e.getMessage());
		}

	}

}
